package com.zeek.car90s.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zeek.car90s.Constants;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2016/4/17.
 */
public class DbHelper {

    private static SQLiteDatabase db;
    public static final String DB_NAME = "mingren.db";

    public static SQLiteDatabase getDataBase(Context context) {
        if (context == null) {
            return null;
        }

        String path = context.getFilesDir().toString() + "/" + DB_NAME;
        File file = new File(path);
        //查看数据库文件是否存在，如果版本更新了就必须重新读取数据库
        if (file.exists() && (!isNewVersion(context))) {
//            Log.i("test", "存在数据库");
            //存在则直接返回打开的数据库
            if (db == null) {
                db = SQLiteDatabase.openOrCreateDatabase(path, null);
            }
        } else {
            try {
                //得到资源
                AssetManager am = context.getAssets();
                //得到数据库的输入流
                InputStream is = am.open(DB_NAME);
                //用输出流写到SDcard上面
                FileOutputStream fos = new FileOutputStream(path);
                //创建byte数组  用于1KB写一次
                byte[] buffer = new byte[1024];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    Log.i("test", "得到");
                    fos.write(buffer, 0, count);
                }
                //最后关闭就可以了
                fos.flush();
                fos.close();
                is.close();

                db = SQLiteDatabase.openOrCreateDatabase(path, null);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

//            if (db == null) {
//                String path = this.getActivity().getFilesDir().toString() + "/libaiquanji.db3";
//                db = SQLiteDatabase.openOrCreateDatabase(path, null);
//            }
        return db;
    }


    private static boolean isNewVersion(Context context) {
        boolean result = false;

        SharedPreferences sp = context.getSharedPreferences(Constants.SHARED_PREFERENCE_PREFIX, Context.MODE_PRIVATE);
        Constants.CURRENT_VERSION_CODE=sp.getInt(Constants.CURRENT_VERSION_CODE_PREFIX,1);

        //获取当前版本号
        PackageInfo pInfo = null;
        try {
            pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        int versionCode = pInfo.versionCode;

        if (versionCode > Constants.CURRENT_VERSION_CODE) {
            Constants.CURRENT_VERSION_CODE = versionCode;
            result = true;

           SharedPreferences.Editor editor = sp.edit();
            editor.putInt(Constants.CURRENT_VERSION_CODE_PREFIX, Constants.CURRENT_VERSION_CODE);
            editor.commit();
        }

        return result;
    }
}
